package com.interview.ujiuye4;

import java.sql.CallableStatement;
import java.util.concurrent.*;

/**
 * Copyright (C), 2018-2020
 * FileName: ThreadPoolDemo1
 * Author:   kongfanyu
 * Date:     2020/10/14 14:42
 */
public class ThreadPoolDemo1 {
    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2,   //核心线程数
                5, //最大线程数
                1L,   //空闲存活时间
                TimeUnit.SECONDS,   //存活时间单位
                new ArrayBlockingQueue<>(3),//阻塞队列
                Executors.defaultThreadFactory(),  //线程工厂 : 创建线程的，HR工厂招人
                //new ThreadPoolExecutor.AbortPolicy() //拒绝策略: 抛异常
                //new ThreadPoolExecutor.CallerRunsPolicy() //拒绝策略: 调用者执行
                //new ThreadPoolExecutor.DiscardPolicy()//拒绝策略:丢弃任务，不抛异常
                new ThreadPoolExecutor.DiscardOldestPolicy()//拒绝策略: 丢弃最久任务
        );

        for (int i = 0; i < 10; i++) {
            final int tempInt = i;
            executor.execute( () ->{
                System.out.println(Thread.currentThread().getName() +"正在给第" + tempInt + "个客户办理业务....."  );
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
            } );

        }
        executor.shutdown();
    }

    private static void test1() {
        //ExecutorService executorService = Executors.newSingleThreadExecutor();
        //ExecutorService executorService = Executors.newFixedThreadPool(5);
        //ExecutorService executorService = Executors.newCachedThreadPool();
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);
        for (int i = 0; i < 10; i++) {
            final int tempInt = i;
            executorService.execute( ()->{
                System.out.println(Thread.currentThread().getName() +"在办理第" + tempInt + "笔业务");
            } );
        }

        executorService.shutdown();
    }
}
